package mortal.game.control
{
    import Message.Db.Tables.*;
    import Message.Public.*;
    import flash.geom.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.ai.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.fishing.*;
    import mortal.game.view.fishing.defin.*;
    import mortal.game.view.fishing.roastFish.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class FishController extends Controller
    {
        private var _fishModule:FishModule;
        private var fishPoint:Point;
        private var _isHaveShowRoastFish:Boolean = false;
        private var _roastFishItemData:ItemData;
        private static const fishSkillId:int = 2000003;

        public function FishController()
        {
            
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.FishRequestLearnSkill, this.onLearnFishSkill);
            Dispatcher.addEventListener(EventName.FishGotoFish, this.onFishGotoFish);
            Dispatcher.addEventListener(EventName.FishStartFish, this.onStartFish);
            Dispatcher.addEventListener(EventName.FishEndFish, this.onEndFish);
            Dispatcher.addEventListener(EventName.FishFishOne, this.onFishOne);
            Dispatcher.addEventListener(EventName.FishPickItem, this.onFishPickItem);
            NetDispatcher.addCmdListener(ServerCommand.SkillFishLeared, this.onSkillFishLeared);
            NetDispatcher.addCmdListener(ServerCommand.FishStartFish, this.onStartFishSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FishEndFish, this.onEndFishSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FishNumChange, this.onFishNumChange);
            NetDispatcher.addCmdListener(ServerCommand.FishItemsUpdate, this.onFishItemsUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChange);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChange);
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.updateIconView);
            Dispatcher.addEventListener(EventName.StartRoastFish, this.onStartRoastFish);
            Dispatcher.addEventListener(EventName.EndRoastFish, this.onEndRoastFish);
            NetDispatcher.addCmdListener(ServerCommand.FishStartRoastFish, this.onStartRoastFishSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FishEndRoastFish, this.onEndRoastFishSuccess);
            
        }

        private function removeFishIconListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.EntityInfo_Update, this.updateIconView);
            
        }

        override protected function initView() : IView
        {
            if (!this._fishModule)
            {
                this._fishModule = new FishModule();
                this._fishModule.addEventListener(WindowEvent.CLOSE, this.onCloseWindow);
            }
            return this._fishModule;
        }

        private function onCloseWindow(event:WindowEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.FishEndFish));
            
        }

        private function onLearnFishSkill(event:DataEvent) : void
        {
            GameProxy.skillProxy.skillLearnReq(fishSkillId);
            
        }

        private function onFishGotoFish(event:DataEvent) : void
        {
            if (RolePlayer.instance.isFishing)
            {
                
            }
            if (RolePlayer.instance.fighting)
            {
                MsgManager.addBroadCast("战斗中，不可使用钓鱼技能");
                
            }
            if (RolePlayer.instance.isTurned)
            {
                MsgManager.addBroadCast("幻化状态，不可使用钓鱼技能");
                
            }
            this.fishPoint = Game.sceneInfo.getFishingNearPoint();
            if (!this.fishPoint)
            {
                MsgManager.addBroadCast("请在太机城水域附近钓鱼");
                
            }
            AIManager.onAIControl(this.fishPoint, AIType.AI_Fish);
            
        }

        private function onStartFish(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPoint;
            GameProxy.fish.startFish(_loc_2);
            
        }

        private function onEndFish(event:DataEvent) : void
        {
            if (cache.fish.isInFishing)
            {
                GameProxy.fish.endFish();
            }
            
        }

        private function onFishOne(event:DataEvent) : void
        {
            var _loc_2:* = event.data as FishingResultDef;
            GameProxy.fish.fish(_loc_2.uid, _loc_2.isHookFish);
            
        }

        private function onFishPickItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as FishPickDef;
            GameProxy.fish.pickFishItem(_loc_2.uid, _loc_2.isPickAll);
            
        }

        private function onSkillFishLeared(param1:SkillInfo) : void
        {
            MsgManager.addBroadCast("获得钓鱼技能");
            var _loc_2:* = cache.skill.getBaseSkillById(fishSkillId);
            Dispatcher.dispatchEvent(new DataEvent(EventName.AddToShortcutsByReq, _loc_2));
            FishIconView.instace.hide();
            
        }

        private function onStartFishSuccess(param1:Object) : void
        {
            view.show();
            
        }

        private function onEndFishSuccess(param1:Object) : void
        {
            view.hide();
            var _loc_2:* = cache.pack.fishPackCache.getItemsByJudge();
            if (_loc_2.length > 0)
            {
                MsgManager.addBroadCast("请及时取出鱼篓中的鱼类");
                FishDropItemWindow.instance.show();
                LayerManager.windowLayer.centerPopup(FishDropItemWindow.instance);
            }
            else
            {
                FishDropItemWindow.instance.hide();
            }
            
        }

        private function onFishNumChange(param1:Object) : void
        {
            (view as FishModule).updateFishNum(cache.fish.fishNum);
            
        }

        private function onFishItemsUpdate(param1:Object) : void
        {
            var _loc_2:* = cache.pack.fishPackCache.getItemsByJudge();
            FishDropItemWindow.instance.updateData(_loc_2);
            if (_loc_2.length > 0)
            {
                (view as FishModule).flicker();
            }
            else
            {
                (view as FishModule).stopFlicker();
            }
            
        }

        private function onPackChange(param1:Object) : void
        {
            (view as FishModule).resetHookMsg();
            
        }

        private function updateIconView(param1:Object) : void
        {
            if (cache.skill.hasBaseSkill(fishSkillId))
            {
                this.removeFishIconListeners();
                FishIconView.instace.hide();
            }
            else
            {
                if (cache.role.entityInfo.level >= 34)
                {
                    FishIconView.instace.show();
                }
                if (cache.role.entityInfo.level >= 35)
                {
                    FishIconView.instace.showLightMask();
                }
            }
            
        }

        private function onStartRoastFish(event:DataEvent) : void
        {
            this._roastFishItemData = event.data as ItemData;
            GameProxy.fish.startRoastFish();
            
        }

        private function onEndRoastFish(event:DataEvent) : void
        {
            GameProxy.fish.endRoastFish(this._roastFishItemData.uid);
            
        }

        private function onStartRoastFishSuccess(param1:Object) : void
        {
            this._isHaveShowRoastFish = true;
            RoashProgressBar.instance.startRoash();
            RoashProgressBar.instance.updateTimers(cache.fish.roastTime, cache.fish.roastMaxTime);
            
        }

        private function onEndRoastFishSuccess(param1:Object) : void
        {
            RoashProgressBar.instance.hide();
            
        }

        public function get isHaveShowRoastFish() : Boolean
        {
            return this._isHaveShowRoastFish;
        }

    }
}
